'use strict';
module.exports = {
	/**
	 * 计算单个投放链接的今日数据
	 */
	main: async (event) => {
		const db = uniCloud.database();
		const _ = db.command;
		const $ = _.aggregate;
		let {
			data = {}, userInfo, util, filterResponse, originalParam
		} = event;
		let {
			customUtil,
			uniID,
			config,
			pubFun,
			vk,
		} = util;
		let {
			uid,
			item
		} = data;
		let res = {
			code: 0,
			msg: ""
		};
		// 业务逻辑开始-----------------------------------------------------------
		// 获取今日00:00:00和23:59:59  
		let {
			todayStart,
			todayEnd
		} = vk.pubfn.getCommonTime(new Date());



		//查询今日新增用户数
		res.num = await vk.baseDao.count({
			dbName: "uni-id-users",
			whereJson: {
				tfid: item._id,
				register_date: _.gte(todayStart)
			}
		});
		//查询今日新用户充值总金额
		res.new_users_moneysum = await vk.baseDao.sum({
			dbName: "a_recharge",
			fieldName: "shifu_amount", // 需要求和的字段名
			whereJson: {
				tfid: item._id,
				pay_status: 1,
				user_register_date: _.gte(todayStart)
			}
		});
		
		//====查询充值订单里，一共有多少个新付费用户====
		let xffyh = await db.collection('a_recharge')
			.aggregate()
			.match({
				tfid: item._id,
				pay_status: 1, //状态为支付完成
				user_register_date: _.gte(todayStart),
				_add_time: _.gte(todayStart)
			})
			.group({
				_id: null,
				user_count: $.addToSet('$user_id')
			})
			.end()
		res.newUsers_payNumber = xffyh.data.length ? xffyh.data[0].user_count.length : 0
		//====计算新付费用户结束====
		


		//查询今日充值笔数 
		res.today_recharges_Number = await vk.baseDao.count({
			dbName: "a_recharge",
			whereJson: {
				tfid: item._id,
				pay_status: 1,
				_add_time: _.gte(todayStart)
			}
		});

		//查询今日充值总金额
		res.today_Recharge_amount = await vk.baseDao.sum({
			dbName: "a_recharge",
			fieldName: "shifu_amount", // 需要求和的字段名
			whereJson: {
				tfid: item._id,
				pay_status: 1,
				_add_time: _.gte(todayStart)
			}
		});
		// 业务逻辑结束-----------------------------------------------------------
		return res;
	}
}
